﻿using Microsoft.AspNetCore.Mvc;
using WorldmeServer.DTO.材质相关;
using WorldmeServer.DTO.游戏相关;
using WorldmeServer.Models.用户相关;
using WorldmeServer.Services.Business.材质相关;
using WorldmeServer.Services.Business.游戏相关;
using WorldmeServer.Services.Business.用户相关;

namespace WorldmeServer.Controllers.材质相关
{
    [ApiController]
    [Route("api/textures")]
    public class TextureController : ControllerBase
    {
        private readonly TextureService textureService;

        public TextureController(TextureService textureService)
        {
            this.textureService = textureService;
        }

        /// <summary>
        /// 根据材质Id 查询材质信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("get-texture/{id}")]
        public async Task<IActionResult> GetTexture(string id)
        {
            var result = await textureService.GetTexture(id);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        /// <summary>
        /// 获取某用户上传的材质
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("get-textures/{userId}")]
        public async Task<IActionResult> GetTextures(string userId)
        {
            var result = await textureService.GetTextures(userId);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpPost]
        [Route("upload")]
        public async Task<IActionResult> Upload([FromForm] IFormFileCollection file, [FromForm] UploadTextureDto dto)
        {
            var user = HttpContext.Items["User"] as User;
            var result = await textureService.Upload(user.Id, file[0], dto);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpDelete]
        [Route("delete/{id}")]
        public async Task<IActionResult> Delete(string id)
        {
            var user = HttpContext.Items["User"] as User;
            var result = await textureService.DeleteTexture(user.Id, id);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpPut]
        [Route("update")]
        public async Task<IActionResult> Update(UpdateTextureDto dto)
        {
            var user = HttpContext.Items["User"] as User;
            var result = await textureService.UpdateTexture(user.Id, dto);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }
    }
}
